//
//  LanguageManager.m
//  MutableLanguage
//
//  Created by bj_szd on 2025/9/3.
//

#import "LanguageManager.h"

static NSString * const kLanguageKey = @"AppLanguage";
static NSString * const kDefaultLanguage = @"zh-Hans";//默认语言是 - 中文
//static NSString * const kDefaultLanguage = @"en";//默认语言是 - 英文
//static NSString * const kDefaultLanguage = @"fr";//默认语言是 - 法语

@interface LanguageManager ()

@property (nonatomic, strong) NSBundle *bundle;

@end

@implementation LanguageManager

+ (instancetype)sharedManager {
    static LanguageManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[LanguageManager alloc] init];
    });
    return manager;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupLanguage];
    }
    return self;
}

- (void)setupLanguage {
    // 打印所有UserDefaults内容用于调试
    NSDictionary *allDefaults = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
    NSLog(@"LanguageManager: 所有UserDefaults内容: %@", allDefaults);
    
    NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:kLanguageKey];
    NSLog(@"LanguageManager: 从UserDefaults读取的语言设置: %@ (key: %@)", language, kLanguageKey);
    
    if (!language) {
        language = kDefaultLanguage;
        [[NSUserDefaults standardUserDefaults] setObject:language forKey:kLanguageKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
        NSLog(@"LanguageManager: 设置默认语言: %@", language);
        
        // 验证设置是否成功
        NSString *savedLanguage = [[NSUserDefaults standardUserDefaults] objectForKey:kLanguageKey];
        NSLog(@"LanguageManager: 验证保存的语言设置: %@", savedLanguage);
    }
    [self setLanguage:language];
}

- (NSString *)currentLanguage {
    return [[NSUserDefaults standardUserDefaults] objectForKey:kLanguageKey] ?: kDefaultLanguage;
}

- (void)setLanguage:(NSString *)language {
    NSLog(@"LanguageManager: 尝试设置语言: %@", language);
    NSString *currentLang = [self currentLanguage];
    NSLog(@"LanguageManager: 当前语言: %@", currentLang);
    
    BOOL languageChanged = !language || ![language isEqualToString:currentLang];
    
    if (!language) {
        NSLog(@"LanguageManager: 语言为空，跳过设置");
        return;
    }
    
    // 保存语言设置（如果需要）
    if (languageChanged) {
        [[NSUserDefaults standardUserDefaults] setObject:language forKey:kLanguageKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
        NSLog(@"LanguageManager: 语言设置已保存到UserDefaults: %@", language);
        
        // 验证保存是否成功
        NSString *savedLanguage = [[NSUserDefaults standardUserDefaults] objectForKey:kLanguageKey];
        NSLog(@"LanguageManager: 验证保存结果: %@", savedLanguage);
    } else {
        NSLog(@"LanguageManager: 语言未改变，但需要设置bundle (新语言: %@, 当前语言: %@)", language, currentLang);
    }
    
    // 设置bundle（无论语言是否改变都需要设置）
    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];
    NSLog(@"LanguageManager: 查找语言资源路径: %@", path);
    if (path) {
        self.bundle = [NSBundle bundleWithPath:path];
        NSLog(@"LanguageManager: 设置bundle路径: %@", path);
        NSLog(@"LanguageManager: Bundle信息: %@", self.bundle);
    } else {
        self.bundle = [NSBundle mainBundle];
        NSLog(@"LanguageManager: 未找到语言资源，使用主bundle: %@", self.bundle);
    }
    
    // 发送语言改变通知（只有在语言真正改变时才发送）
    if (languageChanged) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LanguageChanged" object:nil];
        NSLog(@"LanguageManager: 发送语言改变通知");
    }
}

- (NSString *)localizedStringForKey:(NSString *)key {
    NSString *result;
    if (self.bundle) {
        result = [self.bundle localizedStringForKey:key value:key table:nil];
        NSLog(@"LanguageManager: 从bundle获取本地化字符串 - key: %@, result: %@, bundle: %@", key, result, self.bundle.bundlePath);
    } else {
        result = [[NSBundle mainBundle] localizedStringForKey:key value:key table:nil];
        NSLog(@"LanguageManager: 从主bundle获取本地化字符串 - key: %@, result: %@", key, result);
    }
    return result;
}

- (NSArray<NSString *> *)supportedLanguages {
    return @[@"zh-Hans", @"en", @"fr"];
}

- (NSString *)displayNameForLanguage:(NSString *)language {
    NSDictionary *displayNames = @{
        @"zh-Hans": @"中文",
        @"en": @"English",
        @"fr": @"Français"
    };
    return displayNames[language] ?: language;
}

@end
